package com.zerox.controller;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zerox.domain.TableVo;
import com.zerox.pojo.User;
import com.zerox.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpMediaTypeException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }



    //处理登录的请求
        @PostMapping("/login")
    public String doLogin( String username, String password, Model model,HttpSession session) {

        User login = userService.login(username, password);
        if (login!=null){
            session.setAttribute("loginUser",login);
            return "redirect:user/list2";
        }
        model.addAttribute("errorMsg","用户名或密码错误");
        return "error";


    }
    //局部异常处理
//    @ExceptionHandler(RuntimeException.class)
//    public String handlerException(Model model,RuntimeException e){
//        model.addAttribute("errorMsg",e.getMessage());
//        return "error";
//    }

}
